package com.fingard.dsp.bank.directbank.unionpay01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 合众易宝批量代收付
 * 
 * @author Cruise
 * @date 2019/4/29
 */
public class UnionPay01BatPayRec extends UnionPay01Base implements IBatPayRec {
	@Override
	public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
		retAts.reqSeqID = p_req1988.reqBatchNO;

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1988.ownAct, "bat");
			if (actItem == null) {
				retAts.respCode = "0";
				retAts.respInfo = "未找到账号[" + p_req1988.ownAct + "]商户配置信息!";
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = FailCode.E9981;
				retAts.writeRetToATS();
				return retAts;
			}
			
			UnionPay01Header tmpBankHeader = new UnionPay01Header();
			tmpBankHeader.transCode = UnionPay01TransCode.batPay_1988_198801;
			tmpBankHeader.reqSeqID = p_req1988.reqHeader.reqSeqID;

			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, tmpBankHeader);

			xmlWriter.writeStartElement("BODY");
			xmlWriter.writeStartElement("TRANS_SUM");
			xmlWriter.writeElementString("MERCHANT_ID", actItem.merchantId);
			xmlWriter.writeElementString("MERC_BATCH_NO", p_req1988.reqBatchNO);
			xmlWriter.writeElementString("MERC_BATCH_DATE", Format.Date8Format.format(p_req1988.getPayDateTime()));
			xmlWriter.writeElementString("BUSINESS_CODE", "00600");
			xmlWriter.writeElementString("TOTAL_NUM", p_req1988.totalNum);
			xmlWriter.writeElementString("TOTAL_AMT", p_req1988.totalAmt);
			xmlWriter.writeElementString("NOTIFY_URL", "");
			xmlWriter.writeElementString("TERMINAL_TYPE", "");
			xmlWriter.writeElementString("TERMINALID", "");
			xmlWriter.writeElementString("DEVICE_INFO", "");
			xmlWriter.writeElementString("EXTEND1", "");
			xmlWriter.writeElementString("EXTEND2", "");
			xmlWriter.writeElementString("EXTEND3", "");
			xmlWriter.writeEndElement();
			
			XElement tmpRecords = new XElement("TRANS_DETAILS", "");
			while (p_req1988.readRecord()) {
				XElement tmpRecord = new XElement("TRANS_DETAIL", "");
				tmpRecord.addChild(new XElement("MERC_ORDER_NO", p_req1988.readedDetail.askNumber));
				
				String acctType = "";
				if ("1".equals(p_req1988.readedDetail.isPrivate)) {
					if ("2".equals(p_req1988.readedDetail.cardType)) {
						acctType = "00";
					} else if ("1".equals(p_req1988.readedDetail.cardType)) {
						acctType = "01";
					}
				} else {
					acctType = "02";
				}
				if (StringHelper.isNullOrEmpty(acctType)) {
					acctType = "00";
				}
				tmpRecord.addChild(new XElement("ACCOUNT_TYPE", acctType));
				
				String isPrivate = "";
				if ("1".equals(p_req1988.readedDetail.isPrivate)) {
					isPrivate = "0";
				} else {
					isPrivate = "1";
				}
				tmpRecord.addChild(new XElement("ACCOUNT_PROP", isPrivate));
				tmpRecord.addChild(new XElement("ACCOUNT_NO", p_req1988.readedDetail.recAct));
				tmpRecord.addChild(new XElement("ACCOUNT_NAME", p_req1988.readedDetail.recName));
				tmpRecord.addChild(new XElement("BANK_NO", p_req1988.readedDetail.recBankCode));
				tmpRecord.addChild(new XElement("BANK_NAME", p_req1988.readedDetail.cnapsName));
				tmpRecord.addChild(new XElement("BANK_SHORT_NAME", p_req1988.readedDetail.recBankName));
				tmpRecord.addChild(new XElement("BANK_PROVINCE", p_req1988.readedDetail.getOppProvinceAndCity2()[0]));
				tmpRecord.addChild(new XElement("BANK_CITY", p_req1988.readedDetail.getOppProvinceAndCity2()[1]));
				tmpRecord.addChild(new XElement("TRANS_AMT", p_req1988.readedDetail.payAmount));
				tmpRecord.addChild(new XElement("CURRENCY", "CNY"));
				tmpRecord.addChild(new XElement("REMARK", p_req1988.readedDetail.useType));
				tmpRecord.addChild(new XElement("REMARK2", ""));
				tmpRecord.addChild(new XElement("REMARK3", ""));
				tmpRecord.addChild(new XElement("EXTEND1", ""));
				tmpRecord.addChild(new XElement("EXTEND2", ""));
				tmpRecord.addChild(new XElement("EXTEND3", ""));
				nodeAnalysis(p_req1988.readedDetail.srcXElement, tmpRecord, getCharset());
				tmpRecords.addChild(tmpRecord);
			}
			xmlWriter.writeXElement(tmpRecords);
			xmlWriter.writeEndDocument();
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), actItem, "1988");
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					retAts.respCode = "0";
					retAts.respInfo = tmpBkReader.getFirstText("/AIPG/INFO/RET_MSG");
					retAts.transState = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
					retAts.payInfoCode = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			} else if (ConnState.FAIL.equals(tmpStrRet[0])) {
				retAts.respCode = "0";
				retAts.respInfo = tmpStrRet[1];
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = FailCode.E9999;
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.toString();
			WriteBankLogLn(ex);
		}

		retAts.writeRetToATS();
		return retAts;
	}

	@Override
	public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
		retAts.reqSeqID = p_req1998.reqBatchNO;

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1998.ownAct, "bat");
			if (actItem == null) {
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账号[" + p_req1998.ownAct + "]商户配置信息!";
				retAts.writeAfterDetail();
				return retAts;
			}
			
			UnionPay01Header tmpBankHeader = new UnionPay01Header();
			tmpBankHeader.transCode = UnionPay01TransCode.batPayQry_1998_198801;
			tmpBankHeader.reqSeqID = p_req1998.reqBatchNO;

			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, tmpBankHeader);

			xmlWriter.writeStartElement("BODY");
			xmlWriter.writeElementString("MERCHANT_ID", actItem.merchantId);
			xmlWriter.writeElementString("MERC_BATCH_NO", p_req1998.reqBatchNO);
			xmlWriter.writeElementString("MERC_BATCH_DATE", Format.Date8Format.format(p_req1998.getBgnDateTime()));
			xmlWriter.writeElementString("MERC_ORDER_NO", "");
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), actItem, "1998");

			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					retAts.respCode = "0";
					retAts.respInfo = tmpBkReader.getFirstText("/AIPG/INFO/RET_MSG");

					String tmpRespCode = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
					if ("0000".equals(tmpRespCode)) {
						while (tmpBkReader.read("/AIPG/BODY/RET_DETAILS/RET_DETAIL")) {
							RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
							tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("ACCOUNT_NO");
							tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleText("ACCOUNT_NAME");
							tmpRetDetail.postScript = tmpBkReader.readedXElement.getSingleText("MERC_ORDER_NO");
							tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("TRANS_AMT");
							tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("ERR_MSG");
							tmpRetDetail.result = tmpBkReader.readedXElement.getSingleText("ORDER_STATUS");
							tmpRetDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("ERR_CODE");
							retAts.writeRetDetail(tmpRetDetail);
						}
					} else {
						if ("30040010".equals(tmpRespCode) && retAts.respInfo.contains("商户批次号不存在")) {
							if (canBeNotExist(p_req1998, retAts, 45)) {
								retAts.respCode = "0";
								retAts.transState = StringHelper.joinNotEmpty("+", tmpRespCode, retAts.respInfo);
								retAts.payInfoCode = retAts.respInfo;
							}
						} else {
							retAts.respCode = "-1";
							retAts.respInfo = tmpBkReader.getFirstText("/AIPG/INFO/RET_MSG");
						}
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeAfterDetail();
		return retAts;
	}

	@Override
	public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
		return null;
	}

	@Override
	public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
		return null;
	}
}
